#version 460
#extension GL_GOOGLE_include_directive : enable

#include "../../../../core/shader_tranform.h"
#include "../../../../core/shader_color.h"
#include "../../../../core/shader_widget_panel.h"
#include "../../../../core/shader_widget_color.h"

layout(points) in;
layout(line_strip, max_vertices=4) out;





layout(location = 1) in VS_GS_VERTEX {
	vec2 vPos;
	uint vInstance;
	uint vVertexIndex;
}vertex_in[];


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};

//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};

void main(void){
	mat3x2 mat = m_LP_SD[vertex_in[0].vInstance];

	vec4 color = f_glsl_buildColor(InstanceColor[vertex_in[0].vInstance]);
	if((vertex_in[0].vVertexIndex & 1) <= 0){
		color.a *= 0.6;
	}
	uint VertexIndex = vertex_in[0].vVertexIndex;
	
	
	vec4 v = vec4(vertex_in[0].vPos, 0, 1);
	
	float width = mat[2].x * 2 * m_Scale.x;
	float height = mat[2].y * 2 * m_Scale.y;

	vec2 size = m_Scale * mat[2];
	vec2 scale_gap = size * 2;
	
	vec2 offset = m_Loc - ivec2(m_Loc / scale_gap) * scale_gap - scale_gap;

	//scale_gap += mat[2];

	if((vertNum & 1) > 0) {
		vec4 vH = v;
		vH.x = v.x * size.x;
		vH.x += offset.x;
		
		
		//点1
		vH.x = round(vH.x) + 0.5;
		gl_Position = mat_ProjecView * vH;
		
		gl_PointSize = 1;
		gColor = color;
		EmitVertex();
	
		//点2
		vec4 vH2 = v;
		vH2.x = v.x * size.x;
		vH2.x += offset.x;

		vH2.y += mat[1].y;
		vH2.x = round(vH2.x) + 0.5;
		gl_Position = mat_ProjecView * vH2;
		
		gl_PointSize = 1;
		gColor = color;
		EmitVertex();

		EndPrimitive();
	}
	
	//}

	if((vertNum & 2) > 0){
		vec4 vV = vec4(0,0,0,1);
		//vV.y += float(VertexIndex) * mat[2].y;
		vV.y += float(VertexIndex) * size.y;
		vV.y += offset.y;

		
		//点1
		vV.y = round(vV.y);
		gl_Position = mat_ProjecView * vV;
		
		gl_PointSize = 1;
		gColor = color;
		EmitVertex();

		//点2
		vV.y = float(VertexIndex) * size.y;
		vV.y += offset.y;

		vV.x = mat[1].x;
		vV.y = round(vV.y);
		gl_Position = mat_ProjecView * vV;
		
		gl_PointSize = 1;
		gColor = color;
		EmitVertex();
		EndPrimitive();
	}

}



 